/**
 * 
 */
package com.scpii.universal.service.mobile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.scpii.api.common.auth.token.TokenService;
import com.scpii.api.domain.auth.UsersInfoDetails;
import com.scpii.universal.hibernate.entity.AppDetailPageTab;
import com.scpii.universal.hibernate.entity.AppPageViewTab;
import com.scpii.universal.hibernate.entity.AppViewCatelogRalationTab;
import com.scpii.universal.hibernate.entity.AppViewContentRalationTab;
import com.scpii.universal.hibernate.entity.AttributeTab;
import com.scpii.universal.hibernate.entity.BindContentAttributeTab;
import com.scpii.universal.hibernate.entity.CatelogTab;
import com.scpii.universal.hibernate.entity.ContentTab;
import com.scpii.universal.hibernate.entity.SortColumnPOJO;
import com.scpii.universal.hibernate.entity.SortCondition;
import com.scpii.universal.hibernate.entity.ViewContentAttributeTab;
import com.scpii.universal.pb.PbUniversal.PBRESULT;
import com.scpii.universal.pb.PbUniversal.PbAppPageCatelogSC;
import com.scpii.universal.pb.PbUniversal.PbPageContentPageCS;
import com.scpii.universal.pb.PbUniversal.PbPageContentPageSC;
import com.scpii.universal.pb.PbUniversal.PbViewCatelogPageCS;
import com.scpii.universal.pb.PbUniversal.PbViewCatelogPageSC;
import com.scpii.universal.pb.PbUniversal.PbViewContentPageCS;
import com.scpii.universal.pb.PbUniversal.PbViewContentPageSC;
import com.scpii.universal.pb.PbUniversalBasic.PbAppPageContent;
import com.scpii.universal.pb.PbUniversalBasic.PbAppPageContentList;
import com.scpii.universal.pb.PbUniversalBasic.PbContentAttributeValue;
import com.scpii.universal.pb.PbUniversalBasic.PbContentAttributeValueList;
import com.scpii.universal.pb.PbUniversalBasic.PbPageViewCatelog;
import com.scpii.universal.pb.PbUniversalBasic.PbPageViewCatelogList;
import com.scpii.universal.pns.hibernate.entity.UserActions;
import com.scpii.universal.pns.service.UserActionsService;
import com.scpii.universal.service.AppDetailPageTabService;
import com.scpii.universal.service.AppViewCatelogRalationTabService;
import com.scpii.universal.service.AppViewContentRelationTabService;
import com.scpii.universal.service.AttributeTabService;
import com.scpii.universal.service.AttributeValueTabService;
import com.scpii.universal.service.BindContentAttributeTabService;
import com.scpii.universal.service.CatelogTabService;
import com.scpii.universal.service.ContentTabService;
import com.scpii.universal.service.ViewContentAttributeTabService;
import com.scpii.universal.util.Const;

/**
 * UniversalApi
 * @author 甘建新
 * 2012-8-10下午03:15:44
 */
public class MobilePageService {
	private AppViewContentRelationTabService appViewContentRelationTabService;
	private AppViewCatelogRalationTabService appViewCatelogRalationTabService;
	private CatelogTabService catelogTabService;
	private ViewContentAttributeTabService viewContentAttributeTabService;
	private AttributeTabService attributeTabService;
	private ContentTabService contentTabService;
	private AttributeValueTabService attributeValueTabService;
	private AppDetailPageTabService appDetailPageTabService;
	private BindContentAttributeTabService bindContentAttributeTabService;
	private TokenService tokenService;
	private UserActionsService userActionsService;
	
	private Logger log = Logger.getLogger(MobilePageService.class);
	private String LOG_METHOD_NAME="";
	/**
	 * 分页获取视图中绑定的分类列表
	 * @param bytes
	 * @return
	 * @author 甘建新
	 * 2012-8-10下午03:17:25
	 */
	public byte[] getViewCatelogListByPageNum(byte[] bytes){
		LOG_METHOD_NAME="getViewCatelogListByPageNum";
		
		PbViewCatelogPageSC.Builder pbViewCatePageSCB = PbViewCatelogPageSC.newBuilder();
		PbPageViewCatelogList.Builder pbPageViewCateListB = PbPageViewCatelogList.newBuilder();
		if (bytes!=null && bytes.length>0) {
			try {
				PbViewCatelogPageCS pbViewCatePageCS = PbViewCatelogPageCS.parseFrom(bytes);
				int pageNum = pbViewCatePageCS.getIntPageNum();
				int viewId = pbViewCatePageCS.getIntViewId();
				
				log.info(LOG_METHOD_NAME+"【pageNum:"+pageNum+",viewId:"+viewId+"】");
				
				List<AppViewCatelogRalationTab> appViewCateRList = appViewCatelogRalationTabService.getViewCateListByViewId(viewId, pageNum);
				if (appViewCateRList!=null && appViewCateRList.size()>0) {
					for (AppViewCatelogRalationTab appViewCatelogRalationTab : appViewCateRList) {
						int catelogId = appViewCatelogRalationTab.getIntCateId();
						if (catelogId>0) {
							CatelogTab catelogTab = catelogTabService.get(catelogId);
							if (catelogTab!=null) {
								PbPageViewCatelog.Builder pbPageViewCatelogB = PbPageViewCatelog.newBuilder();
								pbPageViewCatelogB.setIntCateId(catelogId);
								pbPageViewCatelogB.setIntNextPageType(appViewCatelogRalationTab.getIntContentType());
								pbPageViewCatelogB.setIntResourceId(appViewCatelogRalationTab.getIntResourceId());
								pbPageViewCatelogB.setIntViewCateId(catelogTab.getIntCateId());
								pbPageViewCatelogB.setIntViewId(appViewCatelogRalationTab.getIntViewId());
								
								pbPageViewCatelogB.setVarCateDescrip(catelogTab.getVarCateDescrip());
								pbPageViewCatelogB.setVarCateImgUrl(catelogTab.getVarCateImgUrl());
								pbPageViewCatelogB.setVarCateName(catelogTab.getVarCateName()!=null?catelogTab.getVarCateName():"");
								
								//获得绑定的属性列表
								PbContentAttributeValueList.Builder pbContAVListB = PbContentAttributeValueList.newBuilder();
								List<ViewContentAttributeTab> viewContAttriList = viewContentAttributeTabService.getViewContentAttributeList(viewId);
								if (viewContAttriList!=null && viewContAttriList.size()>0) {
									for (ViewContentAttributeTab viewContentAttributeTab : viewContAttriList) {
										int attriId = viewContentAttributeTab.getIntAttriId();
										int order = viewContentAttributeTab.getIntAttriOrder();
										
										PbContentAttributeValue.Builder pbContAttrValueB = PbContentAttributeValue.newBuilder();
										pbContAttrValueB.setIntAttriId(attriId);
										pbContAttrValueB.setIntAttriOrder(order);
										
										AttributeTab attributeTab = attributeTabService.get(viewContentAttributeTab.getIntAttriId());
										pbContAttrValueB.setVarAttriName(attributeTab.getVarAttriName());
										
										if (viewContentAttributeTab.getIntAttriId() == Const.CATELOG_NAME_ATTRIBUTE_ID) {//如果为分类名称
											pbContAttrValueB.setVarAttriValue(catelogTab.getVarCateName());
										} else if (viewContentAttributeTab.getIntAttriId() == Const.CATELOG_IMG_ATTRIBUTE_ID) {//如果为分类缩略图
											pbContAttrValueB.setVarAttriValue(catelogTab.getVarCateImgUrl());
										}else if(viewContentAttributeTab.getIntAttriId() == Const.CATELOG_DESCRIP_ATTRIBUTE_ID){//如果为分类简介
											pbContAttrValueB.setVarAttriValue(catelogTab.getVarCateDescrip());
										}else {
											pbContAttrValueB.setVarAttriValue("");
										}
										pbContAVListB.addPbContentAttributeValueList(pbContAttrValueB);
									}
								}
								pbPageViewCatelogB.setPbContentAttributeValueList(pbContAVListB);
								pbPageViewCateListB.addPbPageViewCatelog(pbPageViewCatelogB);
							}
						}
					}
					pbViewCatePageSCB.setPbPageViewCatelogList(pbPageViewCateListB);
					pbViewCatePageSCB.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else {
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_NULL);
					pbViewCatePageSCB.setExeResult(PBRESULT.NULL);
				}
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pbViewCatePageSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pbViewCatePageSCB.setExeResult(PBRESULT.FAILED);
		}
		PbViewCatelogPageSC pbViewCatelogPageSC = pbViewCatePageSCB.build();
		return pbViewCatelogPageSC.toByteArray();
	}
	
	/**
	 * 分页获取视图中绑定的内容列表
	 * @param bytes
	 * @return
	 * @author 甘建新
	 * 2012-8-10下午03:17:43
	 */
	public byte[] getViewContentListByPageNum(byte[] bytes){
		LOG_METHOD_NAME="getViewContentListByPageNum";
		
		PbViewContentPageSC.Builder pbViewContentPageSCB = PbViewContentPageSC.newBuilder();
		PbAppPageContentList.Builder pbAppPageContentListB = PbAppPageContentList.newBuilder();
		if (bytes!=null && bytes.length>0) {
			try {
				PbViewContentPageCS pbViewContentPageCS = PbViewContentPageCS.parseFrom(bytes);
				int pageNum = pbViewContentPageCS.getIntPageNum();
				int viewId = pbViewContentPageCS.getIntViewId();
				String accessTokenValue = pbViewContentPageCS.getAccessTokenValue();
				UsersInfoDetails userInfo = tokenService.loadUsersInfoDetails(accessTokenValue);
				long userId = 0;
				if (userInfo != null && !"ANON".equals(userInfo.getUserType())) {
					userId = userInfo.getId();
				}
				
				log.info(LOG_METHOD_NAME+"【pageNum:"+pageNum+",viewId:"+viewId+"】");
				
				//===========================================================================
				AppPageViewTab view = new AppPageViewTab();
				view.setIntViewId(viewId);
				Object[] result = this.pagingGetAppPageContentList(view, pageNum,userId);
				if((Integer)result[0] == 1) {
					//非公共权限，需登录
					log.info("appId:"+userInfo.getAppId()+"userName:"+userInfo.getUserName() + "userId:" + userInfo.getId()+"未登录");
					pbViewContentPageSCB.setExeResult(PBRESULT.NEEDLOGIN);
					return pbViewContentPageSCB.build().toByteArray();
				} else if((Integer)result[0] == 0) {
					pbAppPageContentListB = (PbAppPageContentList.Builder)result[1];
				}
				pbViewContentPageSCB.setPbAppPageContentList(pbAppPageContentListB);
				pbViewContentPageSCB.setExeResult(Const.EXE_RESULT);
				//===========================================================================
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				pbViewContentPageSCB.setExeResult(PBRESULT.EXCEPTION);
				e.printStackTrace();
			}
		}else {
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pbViewContentPageSCB.setExeResult(PBRESULT.FAILED);
		}
		return pbViewContentPageSCB.build().toByteArray();
	}
	
	/**
	 * 分页获取试图中绑定的内容的列表
	 * @param viewId
	 * @param pageNum
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-9-13下午02:30:15
	 */
	public Object[] pagingGetAppPageContentList(AppPageViewTab pageView,int pageNum,long userId)throws Exception{
		Object[] result = new Object[2];//{status,data}(0成功,1需要登录,2绑定的商品列表)
		int viewId = pageView.getIntViewId();
		LOG_METHOD_NAME="pagingGetAppPageContentList";
		log.info(LOG_METHOD_NAME+"【viewId:"+viewId+",pageNum:"+pageNum+"】");
		
		PbAppPageContentList.Builder pbAppPageContentListB = PbAppPageContentList.newBuilder();
		List<AppViewContentRalationTab> appViewContentRList = new ArrayList<AppViewContentRalationTab>();
		//验证视图绑定的数据是什么类型的（分类，还是内容）
		appViewContentRList = appViewContentRelationTabService.getAppViewContentRList(1, viewId);
		if (appViewContentRList!=null && appViewContentRList.size()==1) {
			AppViewContentRalationTab appViewContRTab = appViewContentRList.get(0);
			if (appViewContRTab!=null) {
				int bindType = appViewContRTab.getIntBindType();
				if (bindType == Const.VIEW_BIND_CATELOG) {
					appViewContentRList = appViewContentRList;
				} else if(bindType == Const.VIEW_BIND_CONTENT){
					appViewContentRList = appViewContentRelationTabService.getAppViewContentRList(pageNum, viewId);
				}
			}
		} else {
			appViewContentRList = appViewContentRelationTabService.getAppViewContentRList(pageNum, viewId);
		}
		//对绑定的内容进行分页查询
		if (appViewContentRList!=null && appViewContentRList.size()>0) {
			int appViewContentSize = appViewContentRList.size();
			if (appViewContentSize==1) {
				AppViewContentRalationTab appViewContentRTab = appViewContentRList.get(0);
				int bindType = appViewContentRTab.getIntBindType();
				//为分类
				if (bindType==Const.VIEW_BIND_CATELOG) {
					int appViewContRId = appViewContentRTab.getIntViewContId();
					//获取分类ID
					int catelogId = appViewContentRTab.getIntContentId();
					CatelogTab catelog = catelogTabService.get(catelogId);
					if (catelog!=null) {
						if(userId == 0 && catelog.getIntPrivilege() > CatelogTab.PUBLIC_PRIVILEGE){
							result[0] = 1;
							result[1] = null;
							return result;
						}
						/*if(catelog.getIntType() == 8) {//绑定的商品列表
							result[0] = 2;
							result[1] = catelogId;
							return result;
						}*/
						SortCondition condition = new SortCondition();
						condition.setIntCateId(catelogId);
						condition.setIntPageNum(pageNum);
						condition.setIntViewId(viewId);
						List<ContentTab> contList = contentTabService.getContentListByPage(condition, catelog.getIntAppId());
						if (contList!=null && contList.size()>0) {
							for (ContentTab contentTab : contList) {
								PbAppPageContent.Builder pbAppPageContentB = PbAppPageContent.newBuilder();
								if (catelog.getIntPrivilege() == CatelogTab.PERSON_PRIVILEGE && contentTab.getIntPrivilegeUser() != userId) 
									continue;
								pbAppPageContentB = this.formatPageContent(contentTab, appViewContRId);
								pbAppPageContentListB.addPbAppPageContent(pbAppPageContentB);
							}
						}
					}
				}else if(bindType==Const.VIEW_BIND_CONTENT){//为内容
					pbAppPageContentListB = this.formatContentAttriVList(pageView,appViewContentRList,userId);
				}else {
					Const.EXE_RESULT = PBRESULT.OTHER;
					log.info(LOG_METHOD_NAME+"：其他错误");
				}
			}else if(appViewContentSize>1){
				pbAppPageContentListB = this.formatContentAttriVList(pageView,appViewContentRList,userId);
			}else {
//				pbViewContentPageSCB.setExeResult(PBRESULT.NULL);
				log.info(LOG_METHOD_NAME+Const.LOG_DATA_NULL);
				Const.EXE_RESULT = PBRESULT.NULL;
			}
//			pbViewContentPageSCB.setPbAppPageContentList(pbAppPageContentListB);
			Const.EXE_RESULT = PBRESULT.SUCCESS;
			log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
		}
		result[0] = 0;
		result[1] = pbAppPageContentListB;
		return result;
	}
	
	/**
	 * 应用试图内容列表
	 * @param appViewContRList
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-9-12下午05:30:53
	 */
	private PbAppPageContentList.Builder formatContentAttriVList(AppPageViewTab pageView,List<AppViewContentRalationTab> appViewContRList,long userId)throws Exception{	
		LOG_METHOD_NAME="formatContentAttriVList";
		log.info(LOG_METHOD_NAME+"【List<AppViewContentRalationTab> appViewContRList>】");
		PbAppPageContentList.Builder pbAppPageContListB = PbAppPageContentList.newBuilder();
		if (appViewContRList!=null && appViewContRList.size()>0) {
			for (AppViewContentRalationTab appViewContentRalationTab : appViewContRList) {
				PbAppPageContent.Builder pbAppPageContentB = PbAppPageContent.newBuilder();
				//获得绑定内容的信息
				ContentTab contentTab = contentTabService.get(appViewContentRalationTab.getIntContentId());
				CatelogTab catelog = catelogTabService.get(contentTab.getIntCateId());
				if(userId == 0 && catelog.getIntPrivilege() > CatelogTab.PUBLIC_PRIVILEGE){
					return null;
				}
				if (catelog.getIntPrivilege() == CatelogTab.PERSON_PRIVILEGE && contentTab.getIntPrivilegeUser() != userId) 
					continue;
				//设置页面绑定的内容列表
				pbAppPageContentB = this.formatPageContent(contentTab, appViewContentRalationTab.getIntViewContId());
				if (pageView.isContainStatusBar()){
					pbAppPageContentB.setVarStatisticData(populateContentStatusBar(contentTab.getIntContentId()));
				}
				//设置属性List加到Content中
				pbAppPageContListB.addPbAppPageContent(pbAppPageContentB);
			}
		}
		return pbAppPageContListB;
	}
	
	public String populateContentStatusBar(int contentId){
		int commentNum = 0;
		int viewNum = 0;
		int collectNum = 0;
		int joinNum = 0;
		List result = userActionsService.getStatisticDataByContentId(contentId+Const.CONTENT_ID_PRE);
		for (Object obj : result) {
			Object[] o = (Object[])obj;
			if(UserActions.COMMENTS_ADD.equals(o[1])){
				commentNum = ((java.math.BigDecimal) o[0]).intValue();
			} else if(UserActions.TARGET_VIEW.equals(o[1])){
				viewNum = ((java.math.BigDecimal) o[0]).intValue();
			}else if(UserActions.ACTIVITIES_FAV.equals(o[1])){
				collectNum = ((java.math.BigDecimal) o[0]).intValue();
			}else if(UserActions.ACTIVITIES_JOIN.equals(o[1])){
				joinNum = ((java.math.BigDecimal) o[0]).intValue();
			}
		}
		JSONObject datas = new JSONObject();
		datas.put("commentNum", commentNum);
		datas.put("viewNum", viewNum);
		datas.put("collectNum", collectNum);
		datas.put("joinNum", joinNum);
		
		return datas.toString();
	}
	
	/**
	 * 设置页面绑定内容
	 * @param contentTab
	 * @param viewContentId
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-9-13上午10:00:12
	 */
	private PbAppPageContent.Builder formatPageContent(ContentTab contentTab,int viewContentId)throws Exception{
		LOG_METHOD_NAME="formatPageContent";
		log.info(LOG_METHOD_NAME+"【ContentTab contentTab,viewContentId:"+viewContentId+"】");
		PbAppPageContent.Builder pbAppPageContentB = PbAppPageContent.newBuilder();
		if (contentTab!=null) {
			pbAppPageContentB.setIntAppId(contentTab.getIntAppId());
			pbAppPageContentB.setIntCateId(contentTab.getIntCateId());
			pbAppPageContentB.setIntContentId(contentTab.getIntContentId());
			pbAppPageContentB.setIntEnterpId(contentTab.getIntEnterpId());
			pbAppPageContentB.setIntViewCount(contentTab.getIntViewCount());
			
			pbAppPageContentB.setVarContentDescrip(contentTab.getVarContentDescript());
			pbAppPageContentB.setVarContentImgUrl(contentTab.getVarContentImgUrl());
			pbAppPageContentB.setVarContentName(contentTab.getVarContentName());
			pbAppPageContentB.setVarStatisticData(populateContentStatusBar(contentTab.getIntContentId()));
			pbAppPageContentB.setVarResourceId(contentTab.getIntResourceId().toString());
			
			
			//设置内容详情页面访问地址
			String url ="";
			CatelogTab catelogTab = catelogTabService.getSuperCatelogByCateId(contentTab.getIntCateId());
			int superCatelogId =0;
			if (catelogTab!=null) {
				superCatelogId = catelogTab.getIntCateId();
			}
			pbAppPageContentB.setIntDetailPageType(catelogTab.getIntType());
			AppDetailPageTab appDetailPageTab = appDetailPageTabService.getAppDetailInfo(catelogTab.getIntAppId(), superCatelogId);
			if (appDetailPageTab!=null) {
				url = appDetailPageTab.getVarPageUrl()+contentTab.getIntContentId();
//				pbAppPageContentB.setIntDetailPageType(appDetailPageTab.getIntPageDetailType());
				pbAppPageContentB.setVarDetailPageUrl(url);
			}
			
			//获得绑定内容相关的绑定的属性
			Map<Integer, String> keyValueMap = attributeValueTabService.getAttriIdValueMap(contentTab.getIntContentId());
			List<BindContentAttributeTab> bindContentAttributeList = bindContentAttributeTabService.getBindContentAttributeList(viewContentId);
			PbContentAttributeValueList.Builder pbContentAttrValueListB = PbContentAttributeValueList.newBuilder();
			if (bindContentAttributeList!=null && bindContentAttributeList.size()>0) {
				for (BindContentAttributeTab bindContentAttributeTab : bindContentAttributeList) {
					PbContentAttributeValue.Builder pbContAttrValueB = PbContentAttributeValue.newBuilder();
					//添加时间属性处理
					String value;
					if (bindContentAttributeTab.getIntAttriId() == Const.CATELOG_ADDTIME_ATTRIBUTE_ID) {
						value = contentTab.getAddTimeFormat();
					} else {
						value  = keyValueMap.get(bindContentAttributeTab.getIntAttriId());
					}
					
					AttributeTab attributeTab = attributeTabService.get(bindContentAttributeTab.getIntAttriId());
					
					pbContAttrValueB.setIntAttriId(bindContentAttributeTab.getIntAttriId());
					pbContAttrValueB.setIntContentId(contentTab.getIntContentId());
					pbContAttrValueB.setVarAttriName(attributeTab.getVarAttriName());
					if (value==null) {
						value ="";
					}
					pbContAttrValueB.setVarAttriValue(value);
					pbContAttrValueB.setIntAttriOrder(bindContentAttributeTab.getIntOrder());
					pbContAttrValueB.setIntAttriValueId(0);
					
					pbContentAttrValueListB.addPbContentAttributeValueList(pbContAttrValueB);
				}
			}
			//设置属性List
			pbAppPageContentB.setPbContentAttributeValueList(pbContentAttrValueListB);
		}
		
		return pbAppPageContentB;
	}
	
	/**
	 * 活动页面视图内容列表
	 * @param bytes
	 * @return
	 * @author 甘建新
	 * 2012-8-14下午11:21:08
	 */
	public byte[] getPageViewContentList(byte[] bytes){
		LOG_METHOD_NAME="getPageViewContentList";
		
		PbPageContentPageSC.Builder pbPageContengPageSCB = PbPageContentPageSC.newBuilder();
		PbAppPageContentList.Builder pbAppPageContentListB = PbAppPageContentList.newBuilder();
		
		if (bytes!=null && bytes.length>0) {
			try {
				PbPageContentPageCS pbPageContentPageCS = PbPageContentPageCS.parseFrom(bytes);
				int appId = pbPageContentPageCS.getIntAppId();
				int catelogId = pbPageContentPageCS.getIntCatelogId();
				int pageNum = pbPageContentPageCS.getIntPageNum();
				int viewId = pbPageContentPageCS.getIntViewId();
				
				log.info(LOG_METHOD_NAME+"【appId:"+appId+",catelogId:"+catelogId+",pageNum:"+pageNum+",viewId:"+viewId+"】");
				
				SortCondition condition = new SortCondition();
				condition.setIntCateId(catelogId);
				condition.setIntPageNum(pageNum);
				condition.setIntViewId(viewId);
				List<ContentTab> contentList = contentTabService.getContentListByPage(condition, appId);
				if (contentList!=null && contentList.size()>0) {
					for (ContentTab contentTab : contentList) {
						PbAppPageContent.Builder pbAppPageContB = PbAppPageContent.newBuilder();
						pbAppPageContB.setIntAppId(contentTab.getIntAppId());
						pbAppPageContB.setIntCateId(contentTab.getIntCateId());
						pbAppPageContB.setIntContentId(contentTab.getIntContentId());
						pbAppPageContB.setIntEnterpId(contentTab.getIntEnterpId());
						pbAppPageContB.setIntViewCount(contentTab.getIntViewCount());
						pbAppPageContB.setVarContentDescrip(contentTab.getVarContentDescript());
						
						pbAppPageContB.setVarContentImgUrl(contentTab.getVarContentImgUrl());
						pbAppPageContB.setVarContentName(contentTab.getVarContentName());
						pbAppPageContB.setVarResourceId(contentTab.getIntResourceId().toString());
						
						//设置内容详情页面访问地址
						String url ="";
						CatelogTab catelogTab = catelogTabService.getSuperCatelogByCateId(contentTab.getIntCateId());
						int superCatelogId =0;
						if (catelogTab!=null) {
							superCatelogId = catelogTab.getIntCateId();
						}
						AppDetailPageTab appDetailPageTab = appDetailPageTabService.getAppDetailInfo(appId, superCatelogId);
						if (appDetailPageTab!=null) {
							url = appDetailPageTab.getVarPageUrl()+contentTab.getIntContentId();
						}
						pbAppPageContB.setVarDetailPageUrl(url);
						//设置页面类型
						pbAppPageContB.setIntDetailPageType(catelogTab.getIntType());
						
						pbAppPageContB.setDatAddDate(contentTab.getDatAddDatetime());
						
						//设置内容绑定的属性列表
						//获得属性键值对
						Map<Integer, String> keyValueMap = attributeValueTabService.getAttriIdValueMap(contentTab.getIntContentId());
						PbContentAttributeValueList.Builder pbContAttrVListB = PbContentAttributeValueList.newBuilder();
						List<ViewContentAttributeTab> viewContAttriList = viewContentAttributeTabService.getViewContentAttributeList(viewId);
						if (viewContAttriList!=null && viewContAttriList.size()>0) {
							for (ViewContentAttributeTab viewContentAttributeTab : viewContAttriList) {
								PbContentAttributeValue.Builder pbContAttrValueB = PbContentAttributeValue.newBuilder();
								
								AttributeTab attributeTab = attributeTabService.get(viewContentAttributeTab.getIntAttriId());
								if (attributeTab!=null) {
									pbContAttrValueB.setIntAttriId(viewContentAttributeTab.getIntAttriId());
									pbContAttrValueB.setIntContentId(contentTab.getIntContentId());
									pbContAttrValueB.setVarAttriName(attributeTab.getVarAttriName());
									String value  = keyValueMap.get(viewContentAttributeTab.getIntAttriId());
									if (value==null) {
										value ="";
									}
									pbContAttrValueB.setVarAttriValue(value);
									pbContAttrValueB.setIntAttriOrder(viewContentAttributeTab.getIntAttriOrder());
									pbContAttrValueB.setIntAttriValueId(0);
								}
								
								pbContAttrVListB.addPbContentAttributeValueList(pbContAttrValueB);
							}
						}
						pbAppPageContB.setPbContentAttributeValueList(pbContAttrVListB);
						
						pbAppPageContentListB.addPbAppPageContent(pbAppPageContB);
					}
					pbPageContengPageSCB.setPbAppPageContentList(pbAppPageContentListB);
					pbPageContengPageSCB.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else {
					pbPageContengPageSCB.setExeResult(PBRESULT.NULL);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_NULL);
				}
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pbPageContengPageSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pbPageContengPageSCB.setExeResult(PBRESULT.FAILED);
		}
		return pbPageContengPageSCB.build().toByteArray();
	}
	
	//Setter

	public void setAppViewContentRelationTabService(
			AppViewContentRelationTabService appViewContentRelationTabService) {
		this.appViewContentRelationTabService = appViewContentRelationTabService;
	}

	public void setAppViewCatelogRalationTabService(
			AppViewCatelogRalationTabService appViewCatelogRalationTabService) {
		this.appViewCatelogRalationTabService = appViewCatelogRalationTabService;
	}

	public void setCatelogTabService(CatelogTabService catelogTabService) {
		this.catelogTabService = catelogTabService;
	}

	public void setViewContentAttributeTabService(
			ViewContentAttributeTabService viewContentAttributeTabService) {
		this.viewContentAttributeTabService = viewContentAttributeTabService;
	}

	public void setAttributeTabService(AttributeTabService attributeTabService) {
		this.attributeTabService = attributeTabService;
	}

	public void setContentTabService(ContentTabService contentTabService) {
		this.contentTabService = contentTabService;
	}

	public void setAttributeValueTabService(
			AttributeValueTabService attributeValueTabService) {
		this.attributeValueTabService = attributeValueTabService;
	}

	public void setAppDetailPageTabService(
			AppDetailPageTabService appDetailPageTabService) {
		this.appDetailPageTabService = appDetailPageTabService;
	}

	public void setBindContentAttributeTabService(
			BindContentAttributeTabService bindContentAttributeTabService) {
		this.bindContentAttributeTabService = bindContentAttributeTabService;
	}

	public void setTokenService(TokenService tokenService) {
		this.tokenService = tokenService;
	}

	public void setUserActionsService(UserActionsService userActionsService) {
		this.userActionsService = userActionsService;
	}
}
